package j.framework.tags.form;

import j.framework.tags.TagUtils;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 所有的form控件都继承此类
 * User: jason
 * Date: 2014/12/16 13:38
 */
public abstract class BaseFormControl extends TagSupport {
    protected String name;
    private String dataArgs;
    private boolean required = false;//是否必填项
    private boolean readonly = false;//是否只读
    private boolean disabled = false;//是否禁用
    private String style;
    private String cssClassName;
    private String title;
    private Boolean imeDisabled;

    public abstract void writeFormTagBegin(FormTagWriter writer) throws JspException;
    public abstract void writeFormTagProperties(FormTagWriter writer) throws JspException;
    public abstract void endBeginTag(FormTagWriter writer) throws JspException;
    public abstract String getCssClassName();

    protected Object getFormDataValue(){
        FormTag formTag = TagUtils.getParentAsFormTag(this);
        if(null!=formTag){
            return formTag.getFormDataValue(name);
        }
        return null;
    }

    @Override
    public int doStartTag() throws JspException {
        FormTag formTag = TagUtils.getParentAsFormTag(this);
        FormTagWriter writer;
        if(null!=formTag){
            writer = formTag.getWriter();
        }else{
            writer = new FormTagWriter(this.pageContext.getOut());
        }
        //先写入标签的开始
        writeFormTagBegin(writer);
        //写入标签的ID及名称，即name
        writer.writeFormTagProperty("id", name);
        writer.writeFormTagProperty("name", name);
        writer.writeFormTagProperty("data-args",dataArgs);
        //由子类写入标签的个性部分
        writeFormTagProperties(writer);
        String className = getCssClassName();
        if(StringUtils.isNotEmpty(cssClassName)){
            if(StringUtils.isNullOrEmpty(className)){
                className = cssClassName;
            }else{
                className += " "+cssClassName;
            }
        }
        writer.writeFormTagProperty("class", className);
        if(required){
            writer.writeSomething(" required");
        }
        if(readonly){
            writer.writeSomething(" readonly");
        }
        if(disabled){
            writer.writeSomething(" disabled");
        }
        writer.writeFormTagProperty("title",title);
        String _style;
        if(null != imeDisabled && imeDisabled){
            _style="ime-mode:disabled;";
        }else{
            _style = "";
        }
        if(StringUtils.isNotEmpty(style)){
            _style += style;
        }else{
            if(!(this instanceof CheckboxTag)){
                _style += "width:90%;";
            }
        }
        writer.writeFormTagProperty("style",_style);
        //结束标签的开始写入
        endBeginTag(writer);
        return SKIP_BODY;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDataArgs(String dataArgs) {
        this.dataArgs = dataArgs;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public void setReadonly(boolean readonly) {
        this.readonly = readonly;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void setCssClassName(String cssClassName) {
        this.cssClassName = cssClassName;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setImeDisabled(Boolean imeDisabled) {
        this.imeDisabled = imeDisabled;
    }
}
